package com.tianji.promotion.controller;


import com.tianji.api.dto.promotion.CouponDiscountDTO;
import com.tianji.api.dto.trade.OrderCouponDTO;
import com.tianji.api.dto.trade.OrderCourseDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.query.UserCouponQuery;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.IDiscountSolutionService;
import com.tianji.promotion.service.IUserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户领取优惠券的记录，是真正使用的优惠券信息 前端控制器
 * </p>
 *
 * @author LYQ
 * @since 2024-07-06
 */
@Api(tags = "用户优惠券管理相关接口")
@RestController
@RequestMapping("/user-coupons")
@RequiredArgsConstructor
public class UserCouponController {
    private final IUserCouponService userCouponService;
    private final IDiscountSolutionService discountService;

    /**
     * 用户领取优惠券
     *
     * @param id
     */
    @ApiOperation("用户领取优惠券接口")
    @PostMapping("/{id}/receive")
    public void addCoupon2User(@PathVariable Long id) {
        userCouponService.addCoupon2User(id);
    }

    /**
     * 兑换码兑换优惠券接口
     *
     * @param code
     */
    @ApiOperation("兑换码兑换优惠券接口")
    @PostMapping("/{code}/exchange")
    public void exchangeCoupon(@PathVariable("code") String code) {
        userCouponService.exchangeCoupon(code);
    }


    /**
     * 用户分页查询我的优惠券接口
     *
     * @param query
     */
    @ApiOperation("用户分页查询我的优惠券接口")
    @GetMapping("/page")
    public PageDTO<CouponVO> queryMyCouponsByPage(UserCouponQuery query) {
        return userCouponService.queryMyCouponsByPage(query);
    }

    /**
     * 查询并计算优惠券减免方案
     *
     * @param courses
     */
    @ApiOperation("查询并计算优惠券减免方案")
    @PostMapping("/available")
    public List<CouponDiscountDTO> findDiscountSolution(@RequestBody List<OrderCourseDTO> courses) {
        return discountService.findDiscountSolution(courses);
    }


    /**
     * 查询对应优惠方案明细
     *
     * @param orderCouponDTO
     */
    @ApiOperation("查询对应优惠方案明细")
    @PostMapping("/discount")
    public CouponDiscountDTO getDiscountDetail(@RequestBody OrderCouponDTO orderCouponDTO) {
        return discountService.getDiscountDetail(orderCouponDTO);
    }


    /**
     * 核销优惠券
     *
     * @param userCouponIds
     */
    @ApiOperation("核销优惠券")
    @PutMapping("/use")
    public void useUserCoupon(@RequestBody List<Long> userCouponIds) {
        userCouponService.useUserCoupon(userCouponIds);
    }
}
